<?php
/**
 * Created by 攻略类.
 * User: 尚金殿
 * Date: 2018/8/25
 * Time: 14:44
 */

namespace app\admin\controller;


use think\Db;

class PlaneController extends CommonController
{
    protected $mysql ='plane';
    protected $pk = 'id';
    protected $name = 'name';
    protected $status = 'status';

    //航线列表
    public function lists(){
        $keyWord = $this->request->param('key_word');
        //关键词
        if(!empty($keyWord)){
            $res = Db::name($this->mysql)->where($this->name,'like',$keyWord.'%')->where($this->status,'>',-1)->paginate(10);
        }else{
            $res = Db::name($this->mysql)->where($this->status,'>',-1)->paginate(10);
        }

        //变量输出
        $this->assign(['key_word'=>$keyWord,'res'=>$res]);

        //加载模版
        return $this->fetch();
    }


    public function add(){
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param();
            if(!$data['title']){
                return show(0,'航班标题不能为空');
            }
            if( $data['cid'] == 0  ){
                return show(0,'请选择分类');
            }
            if( !$data['cover']  ){
                return show(0,'未获取到图片信息');
            }
            if( !$data['des']  ){
                return show(0,'请填写机票信息');
            }

//            $data['date'] = strtotime($data['date'] );
            $data['create_time'] = time();
            $data['update_time'] = time();

            if( isset($data['id'])){
                $planeId = $this->save($data);
            }else{
                //入库
                $planeId = $this->getAdd($data);

            }

            if(!$planeId){
                return show(0,'操作失败');
            }
            return show(1,'操作成功');

        }else{
            //加载模版
            return $this->fetch();
        }
    }

    //编辑航线
    public function edit(){
        $id = $this->request->param('id');
        //获取攻略数据
        $plane = Db::name($this->mysql)->where($this->pk,$id)->find();
        //变量输出
        $this->assign(['plane'=>$plane]);
        //加载模版
        return $this->fetch();
    }

    //保存编辑
    public function save($data){
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }
        $data['status']= 1;
        $data['update_time'] = time();
        $id = $data['id'];
        unset($data['id']);
        //入库
        $res = Db::name('plane')->where($this->pk,$id)->update($data);

        if(!$res){
            return show(0,'更新失败');
        }
            return show(1,'更新成功');
    }

    public function setStatus()
    {
        //修改数据
        $res = Db::name('plane')->update($_POST);
        if($res){
            return show(1,'删除成功');
        }
        return show(0,'删除失败');
    }
/**********************************************************************/
    public function PlaneLists()
    {

        $res = Db::name('plane_date')->alias('a')
            ->join('t_plane b ','a.plane_id = b.id','left')->field('a.*,b.title')
            ->where('a.status','eq',1)->paginate(10);

        $this->assign('res',$res);
        return $this->fetch();
    }

    public function planeadd()
    {
        if($this->request->isPost()){
            $data = $this->request->param();
            $data['date'] = strtotime($data['date']);
            $nowtime = strtotime(time());
            if( $data['plane_id'] == 0 ){
                return show(0,'请选择航线');
            }
            if( !$data['date'] ){
                return show(0,'请选择日期');
            }
            if( $data['date'] < $nowtime){
                return show(0,'日期小于当前时间');
            }
            if( !$data['price'] ){
                return show(0,'请填写价格');
            }
            $data['create_time'] = time();
            $data['update_time'] = time();
            if( isset($data['id'])){
                $DateId = $this->planesave($data);
            }else{
                //入库
                $DateId = Db::name('plane_date')->insertGetId($data);
            }
            if(!$DateId){
                return show(0,'操作失败');
            }
            return show(1,'操作成功');

        }else{
            $plane = Db::name('plane')->where('status','eq',1)->select();
            $this->assign('plane',$plane);
            return $this->fetch();
        }

    }

    public function planeedit()
    {
        $id = $this->request->param('id');
        $plane_date = Db::name('plane_date')->find($id);
        $plane_date['date'] = date('Y-m-d',$plane_date['date']);
        $plane = Db::name('plane')->where('status','eq',1)->select();
        $this->assign(['plane'=>$plane,'plane_date'=>$plane_date]);
        return $this->fetch();

    }

    public function planesave($data){
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }
        $data['status']= 1;
        $data['update_time'] = time();
        $id = $data['id'];
        unset($data['id']);
        //入库
        $res = Db::name('plane_date')->where($this->pk,$id)->update($data);

        if(!$res){
            return show(0,'更新失败');
        }
        return show(1,'更新成功');
    }

    public function planesetStatus()
    {
        //修改数据
        $res = Db::name('plane_date')->update($_POST);
        if($res){
            return show(1,'删除成功');
        }
        return show(0,'删除失败');
    }
}